<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\UserRecord;
use app\admin\model\User;
use app\admin\model\App;
use app\admin\model\UserLoginLog;
use think\Db;

class Account extends Api
{

    protected $noNeedLogin = ['updateAccount','getAccount'];
    //protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 更新账户
     *
     * @param int $uid  id
     * @param int $type 增减类型
     * @param int $action 来源类型:0=签到,1=转盘,2=任务,3=SDK
     * @param int $coin 金币
     * @param decimal $dollar 美元
     * @param int $card 提现卡
     */
    public function updateAccount()
    {

        $this->checkParam(['type','action']);
        $action = $this->request->param('action');
        // $chang = $this->request->param('type') == 0 ? '+' : '-';
        $type = $this->request->param('type');
        $coin = $this->request->param('coin');
        $dollar = $this->request->param('dollar');
        $card = $this->request->param('card');
        //$this->checkParam('token', 'head');
        /*$token = $this->request->header('token');
        $user = Token::get($token);*/
        $uid = $this->auth->id;
        //$uid = 1;
        Db::startTrans();
        $res = $this->updateUserAccount($uid, $action, $type, ['coin' => $coin, 'dollar' => $dollar, 'card' => $card]);
        if ($res['status']) {
            Db::commit();
            $this->success(__('Update succeeded'));
        } else {
            Db::rollback();
            $this->error(__($res['msg']));
        }

    }

    public function getAccount()
    {
        $uid = $this->auth->id;
       // $uid = 1;
        $data = User::where('id', $uid)->find();
        $data['app'] = App::where('app_id', $data->app_id)->find();


       // dump($data);die;
        $this->success(__('successful'), $data);
    }

    protected function updateUserAccount($uid, $action, $type, $array, $ext=null) {
        $change = $type == 0 ? '+' : '-';
        $change_type = $type == 0 ? 0 : 1;
        $user = User::where('id', $uid)->lock(true)->find();
        if (!$user) {
            return array('status' => false, 'msg' => 'Account error');
        }
        $msg = [
            'coin' => 'Insufficient gold coins',
            'dollar' => 'The dollar is not enough',
            'card' => 'Insufficient withdrawal card'
        ];
        $type = [
            'dollar' => 0,
            'coin' => 1,
            'card' => 2
        ];
        $data = [
            'user_id' => $uid,
            'app_id' => $user['app_id'],
            'action' => $action,
            'change_type' => $change_type,
            'addtime' => date('Y-m-d H:i:s',time()),
            'ext' => $ext
        ];

        foreach ($array as $k => $v) {
            if ($v) {
                $data['type'] = $type[$k];
                if ($change == '-' && $v > $user[$k]) {
                    return array('status' => false, 'msg' => $msg[$k]);
                }
                $data['change_before'] = $user[$k];
                $data['change'] = $change . $v;
                $data['num'] = $v;
                $data['change_after'] = $user[$k] + $data['change'];
                User::where('id',$uid)->update([$k => $data['change_after']]);
                UserRecord::insert($data);
            }
        }
        return array('status' => true);
    }

 /*   public function addUser()
    {
        $seventtime = \fast\Date::unixtime('day', -100);
        $createlist = [];
        $app = [1,2,3,4,5];
        $list = User::column('id');
        //dump($list);die;

        //Db::startTrans();
        for ($i = 0; $i < 100; $i++)
        {
            //$createlist[] = date("Y-m-d H:i:s", $seventtime + ($i * 86400) + mt_rand(20, 86400));


            for ($m = 0; $m < count($list); $m++) {
                //$createtime = date("Y-m-d H:i:s",$seventtime + (mt_rand(0,100) * 86400) + mt_rand(20, 86400));
                $createtime = $seventtime + (mt_rand(0,100) * 86400) + mt_rand(20, 86400);
                User::where('id', $list[$m])->update(['createtime' => $createtime]);
            }

            $password = $username = $this->create_name();
            $ret = $this->auth->register($username, $password, '', '', ['app_id' => array_rand($app), 'createtime' => $createtime]);
            dump($ret);
        }


       // Db::rollback();

    }*/

  /*  public function addLogin()
    {
        $list = User::field('app_id,id,createtime')->select();
        $chunk_result = array_chunk($list, 500);
        dump($chunk_result[0]);
        die;
        //Db::startTrans();
        foreach ($chunk_result[2] as $val) {
            //dump(date("Y-m-d H:i:s", $val['createtime']));
            //$time = mt_rand(0,50);
            $ret = UserLoginLog::insert(['user_id' => $val['id'],
                'app_id' => $val['app_id'],
                'ip' => '127.0.0.1',
                'type' => 2,
                'addtime' => date("Y-m-d H:i:s", $val['createtime'] + (mt_rand(50,100)*86400))
            ]);
            //dump($ret);
        }
        echo '2';
        //Db::rollback();
    }*/

    /*protected function create_name() {
        $chars = "ABCDEFGHIJKLMNOPQRSTUVWXY_Zabcdefghijklmnopqrstuvwxyz-0123456789";
        $leng = rand(5,18);
        $string = '';
        for ( $i = 0; $i < $leng; $i++ )
        {
            $random=mt_rand(0,strlen($chars)-1);
            $string.= $chars[$random];
        }
        return $string;

    }*/
}